﻿using System;
using System.Data;
using Website.Data;

namespace Website
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void LinkButtonUploadImage_Click(object sender, EventArgs e)
        {
            string _FilePath;
            _FilePath = Server.MapPath(@"\web_ppi.png");

            System.IO.FileStream _FileStream;
            _FileStream = new System.IO.FileStream(_FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            System.IO.BinaryReader _BinaryReader;
            _BinaryReader = new System.IO.BinaryReader(_FileStream);

            System.Data.SqlClient.SqlParameter _SqlParameter;
            _SqlParameter = new System.Data.SqlClient.SqlParameter();
            _SqlParameter.ParameterName = "@image";
            _SqlParameter.SqlDbType = SqlDbType.Image;
            _SqlParameter.Value = _BinaryReader.ReadBytes((int)_BinaryReader.BaseStream.Length);

            string _SqlStatement;
            _SqlStatement = "insert into tb_Temp ([image]) values (@image); select scope_identity()";

            System.Data.SqlClient.SqlCommand _SqlCommand;
            _SqlCommand = new System.Data.SqlClient.SqlCommand();
            _SqlCommand.CommandText = _SqlStatement;
            _SqlCommand.Parameters.Add(_SqlParameter);

            int _NewRecordId = default(int);
            using (System.Data.SqlClient.SqlConnection _SqlConnection
                = DataAccess.SqlConnection("BETA"))
            {
                _SqlCommand.Connection = _SqlConnection;

                object _ResultAsObject;
                _ResultAsObject = _SqlCommand.ExecuteScalar();

                if (_ResultAsObject == null)
                    throw new System.NullReferenceException();

                _NewRecordId = int.Parse(_ResultAsObject.ToString());
            }

            Response.Write("<P>Complete: the new image is in tb_Temp with id: " + _NewRecordId.ToString() + "</P>");
        }
    }
}
